import type { FinAccountResp } from '../shop/finAccount'
import type { Order } from '../shop/order'
import type { Refund } from '../shop/refund'
import type { Lesson } from './lesson'
import http from '@/utils/http'
import { StudentResp } from './student'

const BASE_URL = '/education/class'

export interface ChangeRecord {
  id: string
  enrollmentId: string
  studentId: string
  changeType: string
  originalClassId: string
  originalLessonIndexes: number[]
  targetClassId: string
  targetLessonIndexes: number[]
  details: string
  businessId?: string
  lastLessonClassId?: string
  operateSource: string
  operateStatus: string
  lessonLength: number

  createUser: string
  createTime: string
  updateUser: string
  updateTime: string

  createUserName: string
}

export interface ChangeRecordQuery {
  studentId?: string
  grade?: string
  originalClassId?: string
  targetClassId?: string
  changeType?: string
  operateSource?: string
  operateStatus?: string
  createDateFrom?: string
  createDateTo?: string
}

export interface ChangeListResp extends ChangeRecord {
  studentName: string
  originalClassName: string
  targetClassName: string
  originalLeadTeacherId: string
  originalLeadTeacherName: string
  targetLeadTeacherId: string
  targetLeadTeacherName: string
  refund?: string | number
}

export interface ChangeDetailResp extends ChangeRecord {
  studentName: string
  originalClassName: string
  originalLeadTeacherId: string
  originalClassroomId: string
  originalCampusId: string
  originalLesson: Lesson[]

  targetClassName: string
  targetLeadTeacherId: string
  targetClassroomId: string
  targetCampusId: string
  targetLesson: Lesson[]
  refund?: string | number
}

export interface ClassCancelResp extends ChangeRecord {
  student: StudentResp
  campusId: string
  campusName: string
  originalClassName: string
  originalLeadTeacherId: string
  originalLeadTeacherName: string
  refund: Refund
}

export interface ClassCancelDetailResp extends ClassCancelResp {
  order: Order
  orderFinAccount: FinAccountResp
  originalLesson: Lesson[]
  createUserName: string
}

export interface ClassChangeReq {
  oldClassId: string
  newClassId: string
  studentId: string
  changeType: string
  changeLessonIndexList?: number[]
  onlyRemainLesson: boolean
}

export interface ClassCancelQuery {
  studentId?: string
  grade?: string
  classId?: string
  changeType: string
  operateSource?: string
  operateStatus?: string
  dateFrom?: string
  dateTo?: string
}

export interface ClassCancelReq {
  orderItemId: string
  classId: string
  studentId: string
  changeType: string
  refundType: string
  cancelLessonIndexList?: number[]
  onlyRemainLesson: boolean
}

export interface ClassCancelVerifyReq {
  recordId: string
  refundType: string
  finAccountId?: string
}

export interface ClassCancelAuditReq {
  refundType: string
  finAccountId: string
}

// 转班调课
export const changeClass = (req: ClassChangeReq) => {
  return http.post(`${BASE_URL}/change/create`, req)
}

// 查询转班调课
export const getChangePage = (query: ChangeRecordQuery & PageQuery) => {
  return http.get<Page<ChangeListResp[]>>(`${BASE_URL}/change/page`, query)
}
export const getChangeDetail = (id: string | number) => {
  return http.get<ChangeDetailResp>(`${BASE_URL}/change/detail/${id}`)
}

// 查询退班退课
export const getCancelPage = (query: ClassCancelQuery & PageQuery) => {
  return http.get<Page<ClassCancelResp[]>>(`${BASE_URL}/cancel/page`, query)
}
// 查询退班退课详情
export const getCancelDetail = (id: string | number) => {
  return http.get<ClassCancelDetailResp>(`${BASE_URL}/cancel/detail/${id}`)
}
// 申请退班退课
export const createCancelRecord = (req: ClassCancelReq) => {
  return http.post(`${BASE_URL}/cancel/create`, req)
}
// 通过退班退课审批
export const passCancelRecord = (req: ClassCancelVerifyReq) => {
  return http.post(`${BASE_URL}/cancel/pass`, req)
}
// 拒绝退班退课审批
export const rejectCancelRecord = (req: ClassCancelVerifyReq) => {
  return http.post(`${BASE_URL}/cancel/reject`, req)
}
// 撤销调课记录
export const revertChangeRecord = (req: any) => {
  return http.post(`${BASE_URL}/change/revert`, req)
}
